package com.lzy.wzy.service.imp;


import com.lzy.wzy.mapper.UserMapper;
import com.lzy.wzy.model.User;
import com.lzy.wzy.model.UserBean;
import com.lzy.wzy.model.UserPartBean;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class UserServiceImp implements UserDetailsService {


    @Autowired
    private UserMapper userMapper;

    @Override
    public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
        log.info("username {}",s);

        UserPartBean user = userMapper.queryUser(s);
        if (null == user) {
            throw new UsernameNotFoundException("账号不能为空");
        }

        List<String> permission = userMapper.queryUserAllPermission(user.getUserUuid());
        return new UserBean(s, user.getPassword(),user.getUserUuid(),
                AuthorityUtils.createAuthorityList(permission.toArray(new String[0])),user.getUserStatus());
    }
}
